/**
 * Created by 刘玉波 on 2015/12/28.
 */
define(['angular', 'app.directives', 'services/common/base','services/common/user','controllers/jewellery/jewellery'], function (angular, directives) {
    'use strict';

    /**
     * 公共页眉指令
     */
    directives.directive("header", ['$stateParams','CurrentUser','Base','$resource', function ($stateParams,CurrentUser, Base,$resource) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: "/app/partial/common/header.html",
            link: function ($scope) {

                $scope.currentUser=CurrentUser;
                $scope.logout=function(){
                    //curuserService
                    $resource("/logout").get(function(){
                        window.location="/user/login/index";
                    },function(){});

                }

            }
        }
 }])

    /**
     * web公共页眉指令
     */
    directives.directive("webHeader", ['$stateParams','CurrentUser','Base','$resource','$uibModal','$location', function ($stateParams,CurrentUser, Base,$resource,$uibModal,$location) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: "/app/partial/common/web_header.html",
            link: function ($scope) {

                $scope.backStageFlage=false;

                if(CurrentUser!=undefined){
                    angular.forEach(CurrentUser.permCodeList,function(perm){
                        if("backLoginManager"==perm){
                            $scope.backStageFlage=true;
                        }
                    })
                }
                $scope.goToBackStage=function(){
                    window.location="/xdd";
                }
                $scope.currentUser=CurrentUser;
                $scope.logout=function(){
                    $resource("/logout").get(function(){
                        window.location="/user/login/index";
                    },function(){});
                }
                $scope.updatePwd=function(){
                    $uibModal.open({
                        animation: true,
                        size:"md",
                        backdrop:'static',
                        templateUrl: '/app/partial/jewellery/user_update_pwd.html',
                        controller: "updateUserPwdController",
                        resolve: {
                            model:{}
                        }
                    });
                }
                $scope.topClick=function(topType){
                    $scope.topType=topType;
                }
                var topTypes=['orderList','myOldList','goods','shoppingcart','coupon'];

                var url=$location.url();
                for(var i=0;i<topTypes.length;i++){
                    if(url.indexOf(topTypes[i]) >= 0){
                        $scope.topType=topTypes[i];
                    }
                }
                $scope.shoppingCartNum=0;

                $scope.reamName=CurrentUser.realName;

            }
        }
    }])
    /**
     * web公共页眉指令
     */
    directives.directive("webBottom", ['$stateParams','CurrentUser','Base','$resource', function ($stateParams,CurrentUser, Base,$resource) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: "/app/partial/common/web_bottom.html",
            link: function ($scope) {
            }
        }
    }])


    directives.directive("imageDetail", ['$stateParams','CurrentUser','Base','$resource','$location', function ($stateParams,CurrentUser, Base,$resource,$location) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: "/app/partial/common/image_detail.html",
            link: function ($scope) {
                var url=$location.search().url;
                if(url==undefined||url==""){
                   $scope.imageUrl="/app/images/goodsDefault.gif";
                }else{
                    $scope.imageUrl="/dapi/download/getImageIO?imgUrl="+url;
                }
            }
        }
    }])
    /**
     * 公共页脚指令
     */
/*
    directives.directive("footer", [function () {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: "/app/partial/common/footer.html",
            link: function ($scope, $element, $attr) {

            }
        }
    }]);
*/
    /*格式化富文本框显示*/
    directives.directive('ngBindHtmlUnsafe', [function () {
        return function (scope, element, attr) {
            element.addClass('ng-binding').data('$binding', attr.ngBindHtmlUnsafe);
            scope.$watch(attr.ngBindHtmlUnsafe, function ngBindHtmlUnsafeWatchAction(value) {
                element.html(value || '');
            });
        }
    }]);
});